package pers.qianyu.diytomcat.catalina;

import javax.servlet.*;
import java.io.*;
import java.util.*;

/**
 * @author mizzle rain
 * @date 2021-01-16 16:27
 */
public class ApplicationFilterChain implements FilterChain {
    private int pos;
    private List<Filter> filters;
    private Servlet servlet;

    public ApplicationFilterChain(List<Filter> filters, Servlet servlet) {
        this.filters = filters;
        this.servlet = servlet;
        this.pos = 0;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
        if (pos < filters.size()) {
            Filter filter = filters.get(this.pos++);
            filter.doFilter(servletRequest, servletResponse, this);
        } else {
            servlet.service(servletRequest, servletResponse);
        }
    }
}
